package com.lujianfei.powersearch.ui.select

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.ViewModelProvider
import com.lujianfei.powersearch.R
import com.lujianfei.powersearch.base.BaseActivity
import com.lujianfei.powersearch.database.SearchItemModel
import com.lujianfei.powersearch.databinding.ActivitySelectNodeBinding
import com.lujianfei.powersearch.ui.select.viewmodel.SelectNodeViewModel
import com.lujianfei.powersearch.ui.subitem.ActivityStackManager

class SelectNodeActivity: BaseActivity<ActivitySelectNodeBinding>() {

    companion object {
        fun start(context: Activity,searchItemModel: SearchItemModel?, selectId:Long? = -1, requestCode:Int) {
            context.startActivityForResult(Intent(context, SelectNodeActivity::class.java)
                .putExtra("data", searchItemModel)
                .putExtra("requestCode", requestCode)
                .putExtra("selectId", selectId), requestCode)
        }
    }

    var searchItemModel: SearchItemModel? = null
    var stackInfo: ActivityStackManager.StackInfo? = null
    private val selectNodeViewModel: SelectNodeViewModel by lazy { buildViewModel() }
    var requestCode:Int = 0

    override val titleBarResId = R.id.toolbar

    override val menuResId = R.menu.select_item_menu
    override fun resourceId() = R.layout.activity_select_node

    override fun initView() {
        binding?.vm = selectNodeViewModel
    }

    override fun initData() {
        parseIntent()
        binding?.breadCrumbs?.parentId = searchItemModel?.id?:0
    }

    override fun initEvent() {
        selectNodeViewModel.onItemClickCallback = { item ->
            start(this, searchItemModel = item, selectId = selectNodeViewModel.selectId, requestCode = requestCode)
        }
    }

    private fun parseIntent() {
        searchItemModel = intent.getParcelableExtra("data")
        selectNodeViewModel.selectId = intent.getLongExtra("selectId", -1)
        requestCode = intent.getIntExtra("requestCode", 0)
        stackInfo = ActivityStackManager.StackInfo(searchItemModel, this as BaseActivity<ViewDataBinding>)
        ActivityStackManagerSelect.instance.push(stackInfo!!)
        setTitleMiddleText(searchItemModel?.name)
    }

    override fun handleRightButton(itemId: Int) {
        super.handleRightButton(itemId)
        when (itemId) {
            R.id.action_move_here-> {
                setResult(RESULT_OK, Intent().putExtra("data",searchItemModel))
                finish()
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode != RESULT_OK) return
        setResult(RESULT_OK, data)
        finish()
    }

    override fun onResume() {
        super.onResume()
        selectNodeViewModel.loadData(searchItemModel)
    }

    override fun onDestroy() {
        ActivityStackManagerSelect.instance.remove(stackInfo!!)
        super.onDestroy()
    }
}